<script type="text/javascript">
//<!--
	
	
	$(document).ready(function() {
		$("#resultsTable").tablesorter();
	});
	
	var showresults_setting = "";
	$(document).ready(function() {
		$("#tab-results-label").click(function(e){
			e.preventDefault();
			$("#resultsTableBody").html('<tr><td colspan="4">Please wait while the Result List is loading...</td></tr>')
			updateSettings();
			updateresultlist();
		});
	});
	function updateresultlist(){
		$.get("getResults", function(data){
			var JSONobj = eval(data);
			var resultsAccordeonInnerHtml = "";
			jQuery.each(JSONobj, function(i, val) {
				var tdTagStyle;
				if (val.task_id != last_task){
					tdTagStyle = '';
				} else {
					tdTagStyle = 'style="background-color:#E17009;"';
				}
				var first_part = '<tr onclick="set_last_task_result(\''+val.task_id+'\');"><td '+tdTagStyle+'>'+val.name+'</td><td '+tdTagStyle+'>'+val.task_id+'</td>';
				if(val.finish_time != "null"){
					var lastActive = parseMyDate(val.finish_time);
					var showAfter = new Date();
					showAfter.setHours(showAfter.getHours()-parseInt(showresults_setting));
					
					if (lastActive.getTime() > showAfter.getTime()){
						// show "title"
						// show the data ( preview / video / message )
						resultsAccordeonInnerHtml += first_part;
						resultsAccordeonInnerHtml += '<td '+tdTagStyle+' style="color:#00c000;">Finished</td><td '+tdTagStyle+'><a href="'+val.video_link+'">Download Video</a> (right click and choose "save as...")</td></tr>';
					} 
				} else {
					// show "title"
					// show the data ( preview / video / message )
					
					if(val.last_frame != "null"){
						resultsAccordeonInnerHtml += first_part;
						resultsAccordeonInnerHtml += 	'<td '+tdTagStyle+' style="color:#c00000;">Rendering...</td>';
						resultsAccordeonInnerHtml += '<td '+tdTagStyle+'>Check the latest <a href="'+val.frame_link+'">preview screen</a></td>';
						resultsAccordeonInnerHtml += '</tr>';
					}
					//else{
					//	resultsAccordeonInnerHtml += '<td>No preview available yet...</td>';
					//}
				}
			});
			$("#resultsTableBody").html(resultsAccordeonInnerHtml);
			//$("#resultsAccordion").accordion('destroy');	
			//$("#resultsAccordion").accordion();	
	
			//$("#resultsAccordion").accordion('enable');
			//$("#resultsAccordion").accordion({
			//	autoHeight: false
			//});
			//alert(document.getElementById('resultsTableBody').innerHTML);
			$("#resultsTable").trigger("update");
		});
	}
	function set_last_task_result(loc_last_task){
			last_task = loc_last_task;
			// update the monitor list
			updateresultlist();
			// switch monitor tab , task list
			//$("#tabs").tabs('select', 0);
			//$("#monitor_tabs").tabs('select', 0);
	}
	function updateSettings(){
		$.get("settingUpdate", function(data){
			//var accordion_string = data;
			
			var JSONobj = eval(data);
			jQuery.each(JSONobj, function(i, val) {
				if (val.name == "showresults"){
					showresults_setting = val.value;
				}
			});
		});
	}
	function parseMyDate(datestring){
		var date = datestring;
		date = date.replace(/-/g, ":");
		date = date.replace(/ /g, ":");
		var date_array = date.split(":");
		var newDate = new Date(date_array[0],parseInt(date_array[1])-1,date_array[2],date_array[3],date_array[4],date_array[5]);
		return newDate;
	}	
//-->
</script>
	<div id="results_list">
		<table id="resultsTable" class="tablesorter"> 
			<thead> 
				<tr> 
					<th>Name</th> 
					<th>Job ID</th> 
					<th>Status</th> 
					<th>Link</th> 
				</tr> 
			</thead> 
			<tbody id="resultsTableBody"> 
					
			</tbody>
		</table>
	</div>                                                                  
